var crud = require('../../util/crud');
var model = require('../../model/index.js');
var Schedule = model.get('Schedule');
var response = require('../../util/response');
var modules = {};

modules.create = (req, res) => {
   
    Schedule.create(req.body).then((schedule) => {
    	return res.json(response.success({data: schedule}))
    }).catch(err =>{
        console.log('schedule create err, err = ' + err);
        return res.json(response.error('新建课表失败')).end();
    })
}
modules.find = (req, res) => {
    const query = req.query,
        filter = query.filter || {},
        limit = query.limit,
        skip = query.skip;

    Schedule.find(filter).limit(limit).skip(skip).then((schedules) => {
    	return res.json(response.success({data: schedules}))
    }).catch(err => {
        console.log('schedule find err, err = ' + err);
        return res.json(response.error('查找课表失败')).end();
    })
}
modules.update = (req, res) => {
    const filter = req.query;
    const update = req.body;

    Schedule.update(filter, update).then((schedule) => {

    	return res.json(response.success({message: '更新课表成功', data: schedule}))
    }).catch(err => {
        console.log('schedule remove err, err = ' + err);
        return res.json(response.error('更新课表失败')).end();
    })
}
modules.remove = (req, res) => {
    const filter = req.query;

    Schedule.remove(filter).then((result) => {
    	return res.json(response.success({message: '删除课表成功'}))
    }).catch(err =>{
        console.log('schedule remove err, err = ' + err);
        return res.json(response.error('删除课表失败')).end();
    })
}

module.exports = (router) => {
    crud(router, modules);
}